package com.plant.action; 
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import com.plant.entity.PgcHottestFaq;
import com.plant.entity.PgcMonthList;
import com.plant.entity.PgcWeekList;
import com.plant.entity.UgcUser;
import com.plant.service.FuzzySearchService;
import com.plant.service.HelpService;
import com.plant.tool.CalculateDate;
import com.plant.tool.ListMapSort;

/**
		"0 0 12 * * ?"每天中午十二点触发
		"0 15 10 ? * *"每天早上10：15触发
		"0 15 10 * * ?"每天早上10：15触发
		"0 15 10 * * ? *"每天早上10：15触发
		"0 15 10 * * ? 2005" 2005年的每天早上10：15触发
		"0 * 14 * * ?"每天从下午2点开始到2点59分每分钟一次触发
		"0 0/5 14 * * ?"每天从下午2点开始到2：55分结束每5分钟一次触发
		"0 0/5 14,18 * * ?"每天的下午2点至2：55和6点至6点55分两个时间段内每5分钟一次触发
		"0 0-5 14 * * ?"每天14:00至14:05每分钟一次触发
		"0 10,44 14 ? 3 WED"三月的每周三的14：10和14：44触发
		"0 15 10 ? * MON-FRI"每个周一、周二、周三、周四、周五的10：15触发
		"0 15 10 15 * ?"每月15号的10：15触发
		"0 15 10 L * ?"每月的最后一天的10：15触发
		"0 15 10 ? * 6L"每月最后一个周五的10：15
 */
@Component
public class TimingInsertHotfaq { 
	@Resource(name = "fuzzySearchServiceImpl")
	private FuzzySearchService fuzzySearchService;
	@Resource(name="helpServiceImpl")
	private HelpService helpService;

	@Scheduled(cron="0 15 10 ? * *")//
	public void TFaqAndAnswer(){//定时插入最热问答
		PgcHottestFaq hotfaq=new PgcHottestFaq();
		List<Map<String, Object>> allfaq=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> allfaqinf=new ArrayList<Map<String,Object>>();
		List<Object> faqids=new ArrayList<Object>();
		allfaq=fuzzySearchService.ListAllFaqId();
		for (Map<String, Object> map2 : allfaq) {
			Object faqid=map2.get("id_key");
			faqids=fuzzySearchService.AllAnswerIds(faqid);
			if(faqids.size()==0){
				map2.put("count_like", 0);
				map2.put("answer_count", helpService.OneFaqAnswerCount(faqid));
			}else{
				String aids=StringUtils.join(faqids.toArray(),",");
				map2.put("count_like", fuzzySearchService.CountAllFaqAnswerCount(aids));
				map2.put("answer_count", helpService.OneFaqAnswerCount(faqid));
			}
		}
		ListMapSort.Sort(allfaq);
		allfaqinf=ListMapSort.ListSomeInf(allfaq);
		Date date=new Date();
		Timestamp time=new Timestamp(date.getTime());//获取当前时间
		Integer count=0;
		for (Map<String, Object> map2 : allfaqinf) {
			Integer user_id=Integer.parseInt(map2.get("user_id").toString());
			Integer faq_id=Integer.parseInt(map2.get("id_key").toString());
			Integer answer_count=Integer.parseInt(map2.get("answer_count").toString());
			Integer count_like=Integer.parseInt(map2.get("count_like").toString());
			String title=map2.get("title").toString();
			Timestamp faqtime=(Timestamp) map2.get("create_time");
			count=count+1;
			hotfaq.setSort(count);
			hotfaq.setCreateTime(time);
			hotfaq.setFaqAnswerCount(answer_count);
			hotfaq.setFaqAnswerLike(count_like);
			hotfaq.setFaqTitle(title);
			hotfaq.setFaqId(faq_id);
			hotfaq.setUserId(user_id);
			hotfaq.setFaqCreateTime(faqtime);
			fuzzySearchService.AddhotFaq(hotfaq);
		}
	}


	@Scheduled(cron="0 15 10 ? * MON-FRI")//定时插入月排行表
	public void HotMonthPeople(){
		PgcMonthList pm=new PgcMonthList();
		Date date=new Date();
		UgcUser u=new UgcUser();
		String weekfirstday=CalculateDate.getmonthfirst();
		String weeklastday=CalculateDate.getmonthlasttime();
		List<Map<String, Object>> faq=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> temp=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> threeinf=new ArrayList<Map<String,Object>>();
		Set<Integer> set=new HashSet<Integer>();
		faq=fuzzySearchService.HelpedPeople(weekfirstday,weeklastday);
		for (Map<String, Object> map2 : faq) {
			Integer user_id=Integer.parseInt(map2.get("user_id").toString());
			set.add(user_id);
		}
		Iterator<Integer> user_id=set.iterator();
		while (user_id.hasNext()) {
			Map<String,Object> map1=new HashMap<String, Object>();
			Integer integer = (Integer) user_id.next();
			u.setId(integer);
			map1.put("user_id", integer);
			map1.put("helped_people_count", fuzzySearchService.HelpedCount(u));
			map1.put("count_like", fuzzySearchService.OneUserAnswerBylikeCount(u));
			temp.add(map1);
		}
		ListMapSort.Sort(temp);
		if(temp.size()>2){//取三条
			threeinf=ListMapSort.ListThreeInf(temp);
			Timestamp time=new Timestamp(date.getTime());
			for (Map<String, Object> map2 : threeinf) {
				Integer uid=Integer.parseInt(map2.get("user_id").toString());
				Integer faq_like_count=Integer.parseInt(map2.get("count_like").toString());
				Integer helpcount=Integer.parseInt(map2.get("helped_people_count").toString());
				pm.setUserId(uid);
				pm.setFaqLikeCount(faq_like_count);
				pm.setHelpedPeopleCount(helpcount);
				pm.setCreateTime(time);
				fuzzySearchService.AddHotMonthList(pm);
			}
		}
	}
	
	@Scheduled(cron="0 15 10 ? * MON-FRI")//定时插入月排行表
	public void HotPeople(){
		PgcWeekList week=new PgcWeekList();
		UgcUser u=new UgcUser();
		Date date=new Date();
		String weekfirstday=CalculateDate.getTimefirstday(date);
		String weeklastday=CalculateDate.getTimelastDay(date);
		List<Map<String, Object>> faq=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> temp=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> threeinf=new ArrayList<Map<String,Object>>();
		Set<Integer> set=new HashSet<Integer>();
		faq=fuzzySearchService.HelpedPeople(weekfirstday,weeklastday);
		for (Map<String, Object> map2 : faq) {
			Integer user_id=Integer.parseInt(map2.get("user_id").toString());
			set.add(user_id);
		}
		Iterator<Integer> user_id=set.iterator();
		while (user_id.hasNext()) {
			Map<String,Object> map1=new HashMap<String, Object>();
			Integer integer = (Integer) user_id.next();
			u.setId(integer);
			map1.put("user_id", integer);
			map1.put("helped_people_count", fuzzySearchService.HelpedCount(u));
			map1.put("count_like", fuzzySearchService.OneUserAnswerBylikeCount(u));
			temp.add(map1);
		}
		ListMapSort.Sort(temp);
		if(temp.size()>2){//取三条
			threeinf=ListMapSort.ListThreeInf(temp);
			Timestamp time=new Timestamp(date.getTime());
			for (Map<String, Object> map2 : threeinf) {
				Integer uid=Integer.parseInt(map2.get("user_id").toString());
				Integer faq_like_count=Integer.parseInt(map2.get("count_like").toString());
				Integer helpcount=Integer.parseInt(map2.get("helped_people_count").toString());
				week.setUserId(uid);
				week.setFaqLikeCount(faq_like_count);
				week.setHelpedPeopleCount(helpcount);
				week.setCreateTime(time);
				fuzzySearchService.AddHotWeekList(week);
			}
		}

	}
}  
